<template>
  <div class="body">
    <el-form ref="ruleForm" :model="mdl" label-width="130px">
      
        <el-form-item label="商品图片" required>
          <el-upload
            ref="upload"
            list-type="picture-card"
            action
            :file-list="fileList"
            :show-file-list="true"
            :http-request="uploadFile"
            accept="image/gif,image/jpeg,image/jpg,image/png,image/svg"
            :limit="1"
            :on-change="handlePreview"
            :on-exceed="handleExceed"
            :on-remove="handeRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
        <el-form-item label="商品名称" required>
          <el-input
            v-model="mdl.name"
            placeholder="请输入商品名称"
          />
        </el-form-item>
        <el-form-item label="商品进价" required>
          <el-input
            v-model="mdl.costPrice"
            placeholder="请输入商品进价"
          />
        </el-form-item>
        <el-form-item label="商品售价" required>
          <el-input
            v-model="mdl.price"
            placeholder="请输入商品售价"
          />
        </el-form-item>
        <el-form-item label="商品销量" required>
          <el-input
            v-model="mdl.salesVolume"
            placeholder="请输入商品销量"
          />
        </el-form-item>
        <el-form-item label="店铺" required>
          <el-select @change="changeStore" v-model="mdl.storeId" placeholder="请选择">
            <el-option
              v-for="item in storeList"
              :key="item.storeId"
              :label="item.storeName"
              :value="item.storeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" required>
          <el-select v-model="mdl.goodsTypeId" placeholder="请选择">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上下架" required>
          <el-switch
            @change="statusChange"
            v-model="value"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        
      
    </el-form>
    <div class="foot_btn">
      <el-button
        type="primary"
        :loading="submitLoading"
        @click="submitForm('ruleForm')"
      >提交</el-button>
      <el-button @click="handleCloseDrawer">取消</el-button>
    </div>
  </div>
</template>

<script>
import { fileUpload,pageStore,addGoods,updateGoods,getGoodsTypeList } from '@/api/user'
export default {
  name: 'FormModel',
  props: {
    mdl: {
      type: Object,
      required: true
    },
    handleCloseDrawer: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      submitLoading: false,
      fileList: [],
      uploadData: null,
      img: '',
      storeList:[],
      typeList:[],
      value:false
    }
  },
  mounted() {
    if(this.mdl.img){
      this.img = this.mdl.img
      this.fileList.push({
        url: this.mdl.img
      })
    }
    this.getPageStore()
    if(this.mdl.id){
      this.getTypeList(this.mdl.storeId)
    }
  },
  methods: {
    // 上下架
    statusChange(e){
      console.log(e)
      this.value = e
    },

    // 选择店铺
    changeStore(e){
      this.getTypeList(e)
    },

    // 获取分类
    async getTypeList(id){
      await getGoodsTypeList({storeId:id}).then(res=>{
        if(res.statusCode == '00000'){
          console.log(res)
          this.typeList = res.data
        }
      })
    },

    // 店铺列表
    async getPageStore(){
      await pageStore({current:1,size:20}).then(res=>{
        if(res.statusCode == '00000'){
          this.storeList = res.data.records
        }
      })
    },

    // 移除上传图片
    handeRemove(e){
      this.imgList = ''
    },

    // 上传图片
    uploadFile(e) {
      fileUpload(this.uploadData).then((res) => {
        if (res) {
          // this.mdl.roomImg = res.data
          this.img = res.data
          this.$message({
            message: '上传成功',
            type: 'success'
          })
          console.log(res)
        } else {
          this.$message.error(res.status)
        }
      })
    },
    handleExceed(file) {
      this.$message({
        message: '类型图片只能上传一张',
        type: 'warning'
      })
    },
    // 获取上传文件信息
    handlePreview(file) {
      const _this = this
      var formData = new FormData()
      formData.append('file', file.raw)
      _this.uploadData = formData
    },

    // 提交
    submitForm() {
      this.mdl.putaway = this.value?1:0
      this.mdl.img = this.img
      console.log(this.mdl)
      const request = this.mdl.id ? updateGoods(this.mdl) : addGoods(this.mdl)
      request.then((res) => {
        if (res.statusCode === '00000') {
          this.$message({ message: '提交成功', type: 'success' })
          // 关闭弹窗
          this.handleCloseDrawer()
          // 调用父级组件刷新数据
          this.$emit('getList')
        }
      })
        .catch((error) => {
          console.log(error)
        })
        .finally(() => {
        })
    }
  }
}
</script>

<style lang="less" scoped>
.body {
  padding-right: 25px;
  .foot_btn{
    width:150px;
    margin: 0 auto;
  }
}

/deep/ .el-checkbox__label {
  font-size: 12px;
}
</style>
